xquery version "1.0";

declare namespace c="http://exist-db.org/xquery/controller";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace xdb = "http://exist-db.org/xquery/xmldb";

import module namespace xsgi="http://www.webcomposite.com/xsgi";

let $app     := collection($exist:root)/xsgi:app
let $path    := $app//xsgi:path[ends-with(string(@url),$exist:path)]
let $model   := string($app/xsgi:path[@name eq string($path/@model)])
let $logging := if ($app/@logging) then
					(util:log('info',util:serialize($path,())),util:log-system-out(util:serialize($path,())))
				else
					()
return
    if (exists($path) and string($path/@type) eq 'forward') then
        xsgi:forward($path/text())
    else if (exists($path) and string($path/@type) eq 'redirect') then
        xsgi:redirect($path/text())
    else if (exists($path) and string($path/@type) eq 'view') then
		xsgi:view(doc(concat($exist:root,'/',$path/text())), $model, string($path/@content-type) )
    else if (exists($path) and string($path/@type) eq 'model') then
		xsgi:model($path/text())		
    else if (exists($path) and exists($path/@module)) then
		xsgi:invoke(string($path/@prefix),string($path/@ns),string($path/@module),$path/text(),$model)
    else if (exists($path) and $path/@type eq 'inline') then
		xsgi:view($path/node(), $model, string($path/@content-type) )
	else
		<ignore xmlns="http://exist.sourceforge.net/NS/exist">
            <cache-control cache="yes"/>
		</ignore>
